package cn.lg.soar.cms.biz.modules.other.controller;

import cn.lg.soar.cms.api.constant.ModuleConst;
import cn.lg.soar.cms.biz.modules.other.entity.Region;
import cn.lg.soar.cms.biz.modules.other.model.RegionTreeCache;
import cn.lg.soar.cms.biz.modules.other.service.IRegionService;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Tag(name = "区域管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/other/region")
public class RegionController extends BaseController<Region, IRegionService> {

    @Override
    protected Serializable getId(Region entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "区域管理";

    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated Region data) {
        return super.createResult(data);
    }

    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated Region data) {
        return super.updateResult(data);
    }

    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<Region> detail(@RequestParam(name = "id") @Id Long id) {
        return super.detailResult(id);
    }

    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<Region>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @Log(value = "导出" + SUBJECT, type = LogTypeEnum.EXPORT)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel(RegionController.SUBJECT, params, resp);
    }

    @Log(value = "导入" + SUBJECT, type = LogTypeEnum.IMPORT)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

    @Operation(summary = "树形数据")
    @GetMapping("tree")
    public HttpResult<List<RegionTreeCache>> tree(@RequestParam(name = "parentId", required = false) Integer parentId) {
        return HttpResult.data(service.tree(parentId));
    }

}
